package com.cskaoyan.service.vx;

import com.cskaoyan.bean.bo.vxBo.CartAddBo;
import com.cskaoyan.bean.bo.vxBo.CartCheckedBo;
import com.cskaoyan.bean.bo.vxBo.CartUpdateBo;
import com.cskaoyan.bean.po.wxPo.*;
import com.cskaoyan.bean.vo.WxTempVO;
import com.cskaoyan.bean.vo.vxVo.cartVo.*;
import com.cskaoyan.mapper.wxMapper.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @projectName: seeonproject1
 * @package: com.cskaoyan.service.vx
 * @className: CartServiceImpl
 * @author: YangDongpeng
 * @description:
 * @date: 2022/6/7 14:16
 * @version: 1.0
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    MarketCartMapper marketCartMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Override
    public Integer selectgoodscount(MarketUser marketUser) {

        Integer goodscount = marketCartMapper.selectgoodscount(marketUser);
        return goodscount;
    }

    @Override
    public void insertINcart(CartAddBo cartBo, MarketUser marketUser) {
        System.out.println("增加了");
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andIdEqualTo(cartBo.getGoodsId()).andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        MarketGoods marketGood = marketGoods.get(0);

        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        marketGoodsProductExample.createCriteria().andIdEqualTo(cartBo.getProductId()).andDeletedEqualTo(false);
        List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);
        MarketGoodsProduct marketGoodsProduct = marketGoodsProducts.get(0);
        marketCartMapper.insertOneGoodCount(cartBo, marketGoodsProduct, marketGood, marketUser);

    }

    @Override
    public Integer selectOneGoodsCount(CartAddBo cartBo, MarketUser marketUser) {

        Integer goodscount = marketCartMapper.selectOneGoodCount(cartBo, marketUser);
        return goodscount;
    }

    @Override
    public List<Cartgoods> showgoods(MarketUser marketUser) {
        String specifications = "[默,认]";
        List<CartgoodsPo> cartGoodsList = marketCartMapper.queryCart(marketUser);
        Cartgoods cartGoodsVO = null;
        ArrayList<Cartgoods> cartGoodsVOS = new ArrayList<>();
        for (CartgoodsPo cartgoods : cartGoodsList) {
            if (cartgoods.getSpecifications() != null) {
                specifications = cartgoods.getSpecifications();
            }

            String replace = specifications.replace("\"", "");

            List<String> split = Arrays.asList(replace.substring(1, replace.length() - 1).split(", "));
            cartGoodsVO = new Cartgoods(cartgoods.getId(), cartgoods.getUserId(), cartgoods.getGoodsId(), cartgoods.getGoodsSn(), cartgoods.getGoodsName(), cartgoods.getProductId(), cartgoods.getPrice(), cartgoods.getNumber(), split, cartgoods.isChecked(), cartgoods.getPicUrl(), cartgoods.getAddTime(), cartgoods.getUpdateTime(), cartgoods.isDeleted());


            cartGoodsVOS.add(cartGoodsVO);
        }
        return cartGoodsVOS;
    }

    @Override
    public CartTotal showCartTotal(MarketUser marketUser) {
        CartTotal cartTotal = new CartTotal(0, 0, 0, 0);
        if (marketCartMapper.queryCartTotal(marketUser) != null) {
            cartTotal = marketCartMapper.queryCartTotal(marketUser);
        }
        /*  CartTotal  cartTotal = marketCartMapper.queryCartTotal(marketUser);*/
        CartTotal cartTotalCheck = new CartTotal(0, 0, 0, 0);
        if (marketCartMapper.queryCartTotalCheck(marketUser) != null) {
            cartTotalCheck = marketCartMapper.queryCartTotalCheck(marketUser);
        }


        cartTotal.setCheckedGoodsAmount(cartTotalCheck.getCheckedGoodsAmount());
        cartTotal.setCheckedGoodsCount(cartTotalCheck.getCheckedGoodsCount());

        return cartTotal;
    }

    @Override
    public void updateCheckedCart(CartCheckedBo cartCheckedBo, List<Integer> productIDs, MarketUser marketUser) {
        System.out.println(cartCheckedBo.getProductIds().size());
        marketCartMapper.updateChecked(cartCheckedBo, productIDs, marketUser);
    }

    @Override
    public void updateNumber(CartAddBo cartBo, MarketUser marketUser) {
        System.out.println("改数字");
        MarketCartExample marketCartExample = new MarketCartExample();
        marketCartExample.createCriteria().andProductIdEqualTo(cartBo.getProductId());

        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);
        MarketCart marketCart = marketCarts.get(0);
        int i = marketCart.getNumber() + cartBo.getNumber();
        marketCartMapper.updateNumber(i, cartBo, marketUser);

    }

    @Override
    public Integer selectAdded(CartAddBo cartBo, MarketUser marketUser) {

        int i = 0;
        if (marketCartMapper.selectAdded(cartBo.getProductId(), marketUser) != null) {
            i = marketCartMapper.selectAdded(cartBo.getProductId(), marketUser);
        }

        System.out.println(i + "重复数字是");
        return i;
    }


    @Override
    public void updateDeleteOneCart(List<Integer> productIDs, MarketUser marketUser) {
        marketCartMapper.updateDeleteOneCart(productIDs, marketUser);
    }

    @Override
    public void updateAddNumber(CartUpdateBo cartUpdateBo, MarketUser marketUser) {
        marketCartMapper.updateAddNumber(cartUpdateBo, marketUser);
    }

    @Override
    public CheckedAddress selectAddress(MarketUser marketUser) {
        return null;
    }

    @Override
    public CheckedAddress queryAddressByAddressId(Integer addressId) {
        CheckedAddress checkedAddress = marketAddressMapper.queryAddressInfoById(addressId);
        return checkedAddress;
    }

    @Override
    public List<CheckedGoodsList> showCheckgoods(Integer cartId) {
        String specifications = "[标准]";
        MarketCartExample marketCartExample = new MarketCartExample();
        marketCartExample.createCriteria().andDeletedEqualTo(false).andIdEqualTo(cartId);
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);

        //MarketCart marketCart = marketCartMapper.selectByPrimaryKey(cartId);
        //List<CartgoodsPo> cartGoodsList = marketCartMapper.queryCheckCartGood(cartId);
        CheckedGoodsList cartGoodsVO = null;
        ArrayList<CheckedGoodsList> cartGoodsVOS = new ArrayList<>();
        for (MarketCart cartgoods : marketCarts) {
            if (cartgoods.getSpecifications() != null) {
                specifications = cartgoods.getSpecifications();
            }

            String replace = specifications.replace("\"", "");

            List<String> split = Arrays.asList(replace.substring(1, replace.length() - 1).split(", "));
            cartGoodsVO = new CheckedGoodsList(cartgoods.getId(), cartgoods.getUserId(), cartgoods.getGoodsId(), cartgoods.getGoodsSn(), cartgoods.getGoodsName(), cartgoods.getProductId(), cartgoods.getPrice().intValue(), cartgoods.getNumber(), split, cartgoods.getChecked(), cartgoods.getPicUrl(), cartgoods.getAddTime(), cartgoods.getUpdateTime(), cartgoods.getDeleted());


            cartGoodsVOS.add(cartGoodsVO);
        }
        return cartGoodsVOS;
    }

    @Override
    public WxTempVO findDiscoutBycouponId(Integer total,Integer userId) {
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        marketCouponUserExample.createCriteria().andStartTimeLessThan(new Date()).andEndTimeGreaterThan(new Date()).andDeletedEqualTo(false).andStatusEqualTo((short) 0);
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        BigDecimal min = BigDecimal.valueOf(0);
        Integer cid = 1;
        int count = 0;
        Integer userCid = 0;
        for (MarketCouponUser marketCouponUser : marketCouponUsers) {
            MarketCouponExample marketCouponExample = new MarketCouponExample();
            marketCouponExample.createCriteria().andMinLessThan(BigDecimal.valueOf(total)).andIdEqualTo(marketCouponUser.getCouponId());
            List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
            if (marketCoupons .size() > 0) {
                MarketCoupon marketCoupon = marketCoupons.get(0);
                if (marketCoupon.getDiscount().compareTo(min) > 0) {
                    min = marketCoupon.getDiscount();
                    cid = marketCoupon.getId();
                    count ++;
                    userCid = marketCouponUser.getId();
                }
            }
        }

        return new WxTempVO(min,cid,count,userCid);
    }

    @Override
    public Integer findOrderTotalprice(MarketUser marketUser) {
        Integer totalprice = marketCartMapper.queryOrderTotalprice(marketUser);
        return totalprice;

    }

    @Override
    public Integer selectGoodsCountByUserId(Integer id) {
        MarketCartExample example = new MarketCartExample();
        example.createCriteria().andUserIdEqualTo(id).andDeletedEqualTo(false);
        return (int) marketCartMapper.countByExample(example);
    }

    @Override
    public Integer selectCartTotalPrice(Integer cartId) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        int price = 0;
        if (cartId == 0) {
            MarketCartExample marketCartExample = new MarketCartExample();
            marketCartExample.createCriteria().andCheckedEqualTo(true).andDeletedEqualTo(false).andUserIdEqualTo(marketUser.getId());
            List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);
            for (MarketCart marketCart : marketCarts) {
                price += marketCart.getPrice().intValue() * marketCart.getNumber();
            }
        } else {
            MarketCart marketCart = marketCartMapper.selectByPrimaryKey(cartId);
            price = marketCart.getPrice().intValue() * marketCart.getNumber();
        }
        return price;
    }
}
